package com.jiukuaidao.zhulin.module.user;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.jiukuaidao.library.common.dialog.CustomDialog;
import com.jiukuaidao.library.util.ToastUtil;
import com.jiukuaidao.zhulin.R;
import com.jiukuaidao.zhulin.data.TaskProvider;
import com.jiukuaidao.zhulin.module.CommContract;
import com.jiukuaidao.zhulin.module.CommPresenter;

/**
 * Author : PaiTaSuo
 * Date : 2016/6/20
 * Description : 图形验证码弹框
 */
public class GraphicCodeDialog implements CommContract.GraphicCodeView {

    private CustomDialog mDialog;
    private Context mContext;
    private View mView;
    private EditText mEdtCode;
    private ImageView mIvCode;
    private RelativeLayout mLeftBtn, mRightBtn;
    private CommPresenter mCommPresenter;


    public GraphicCodeDialog(Context context) {
        mContext = context;
        mCommPresenter = new CommPresenter(TaskProvider.provideTasksRepository(mContext), this);
        mCommPresenter.getGraphicCode();
    }

    public void showDialog() {
        mDialog = new CustomDialog(mContext, R.style.customDialogStyle);
        mView = LayoutInflater.from(mContext).inflate(R.layout.graphic_code_layout, null);
        mEdtCode = (EditText) mView.findViewById(R.id.tv_code);
        mIvCode = (ImageView) mView.findViewById(R.id.img_code);
        mLeftBtn = (RelativeLayout) mView.findViewById(R.id.rl_left);
        mRightBtn = (RelativeLayout) mView.findViewById(R.id.rl_right);
        mDialog.setContentView(mView);
        mDialog.show();
        mDialog.setCanceledOnTouchOutside(false);

        mIvCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCommPresenter.getGraphicCode();
            }
        });

        mLeftBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });
        mRightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCommPresenter.validateGraphic();
            }
        });
    }


    @Override
    public void getImgUrl(Bitmap bitmap) {
        mIvCode.setImageBitmap(bitmap);
    }

    @Override
    public String getGraphicCode() {
        return mEdtCode.getText().toString();
    }

    @Override
    public void graphicToast(String msg) {
        ToastUtil.show(msg, Toast.LENGTH_SHORT);
    }

    /**
     * 验证成功
     */
    @Override
    public void validateFinish() {
        mDialog.dismiss();
    }
}

